package com.ctaa.ctaa.serviceimpl;

import com.ctaa.ctaa.dto.Result;
import com.ctaa.ctaa.mapper.AnalysisMapper;
import com.ctaa.ctaa.pojo.AnalysisPojo;
import com.ctaa.ctaa.pojo.CategoryPojo;
import com.ctaa.ctaa.pojo.MonthlyStatisticsPojo;
import com.ctaa.ctaa.service.AnalysisService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AnalysisServiceImpl implements AnalysisService {


    @Resource
    AnalysisMapper analysisMapper;

    //查询今日消费清情况，今日支出、收入、结余
    @Override
    public Result<AnalysisPojo> searchDaySum(Integer userId) {
        if (userId == null) {
            return Result.fail("用户id不能为空");
        }

        //查询今日支出
        Double aDouble = analysisMapper.queryExpDaySumMnoey(userId);
        //查询今日收入
        Double aDouble1 = analysisMapper.queryImpDaySumMnoey(userId);
        //查询总支出
        Double aDouble2 = analysisMapper.queryExpSumMnoey(userId);
        //查询总收入
        Double aDouble3 = analysisMapper.queryImpSumMnoey(userId);
        double v = aDouble3 - aDouble2;//结余
        AnalysisPojo analysisPojo = new AnalysisPojo();
        analysisPojo.setIncome(aDouble1);
        analysisPojo.setSurplus(v);
        analysisPojo.setNsume(aDouble);
        return Result.success("查询成功", analysisPojo);
    }

    @Override
    public Result<AnalysisPojo> searchYearSum(Integer userId, Integer year) {
        if (userId == null) {
            return Result.fail("参数错误");
        }

        String beginTime = null;
        String endTime = null;
        if (year != null) {
            beginTime = year + "-1-1";
            endTime = (year + 1) + "-1-1";
        }


        //查询当前年总支出
        Double expMoney = analysisMapper.queryYearInMnoey(userId, beginTime, endTime, 2);

        //查询当前年总收入
        Double inMoney = analysisMapper.queryYearInMnoey(userId, beginTime, endTime, 1);
        //查询结余
        double v = inMoney - expMoney;
        AnalysisPojo analysisPojo = new AnalysisPojo();
        analysisPojo.setIncome(inMoney);
        analysisPojo.setSurplus(v);
        analysisPojo.setNsume(expMoney);
        return Result.success("查询成功", analysisPojo);
    }

    //通过类别查询支出
    @Override
    public Result<List<CategoryPojo>> searchCategory(Integer userId, Integer year, Integer type) {
        if (userId == null || year == null) {
            return Result.fail("参数错误");
        }
        List<CategoryPojo> stringDoubleMap = analysisMapper.queryCategoryExpMnoey(userId, year + "-1-1", (year + 1) + "-1-1", type);


        return Result.success("查询成功", stringDoubleMap);
    }

    //按月份查询收支情况
    @Override
    public Result<List<MonthlyStatisticsPojo>> searchMonthSum(Integer userId, Integer year) {
        if (userId == null || year == null) {
            return Result.fail("参数错误");
        }
        //查询year收入情况
        List<MonthlyStatisticsPojo> monthIn = analysisMapper.queryMonthSum(userId, year, 1);
        monthIn.forEach(item -> item.setType(1));
        //查询year支出情况
        List<MonthlyStatisticsPojo> monthEx = analysisMapper.queryMonthSum(userId, year, 2);
        monthEx.forEach(item -> item.setType(2));
        monthEx.addAll(monthIn);
        return Result.success("查询成功", monthEx);
    }

    @Override
    public Result<List<String>> searchYear(Integer userId) {
        List<String> strings = analysisMapper.queryYear(userId);
        return Result.success("查询成功", strings);
    }


}
